﻿using System;
using System.Drawing;

namespace MarioMapEditor
{
    class ObBrick : ObObject
    {
        public enum BRICK { BRICK1_BR = 1, BRICK1_GR, BRICK1_NBR, BRICK2_BR, BRICK2_GR, BRICK2_NBR, BRICK3_BR, BRICK3_GR, BRICK3_NBR };

        public ObBrick()
        {
            
        }

        public ObBrick(BRICK nid)
        {
            ID = (int)nid;
        }

        public override bool SetID(int nID)
        {
            if (checkType(nID))
            {
                ID = nID;
                return true;
            }
            return false;
        }

        public static bool checkType(int objectID)
        {
            return Enum.IsDefined(typeof(BRICK), objectID);
        }

        public static int selectBrick(int x, int y)
        {
            switch (y)
            {
                case 0:
                    switch (x)
                    {
                        case 0:
                            return (int)BRICK.BRICK1_BR;
                        case 1:
                            return (int)BRICK.BRICK1_GR;
                        case 2:
                            return (int)BRICK.BRICK1_NBR;
                    }
                    break;
                case 1:
                    switch (x)
                    {
                        case 0:
                            return (int)BRICK.BRICK2_BR;
                        case 1:
                            return (int)BRICK.BRICK2_GR;
                        case 2:
                            return (int)BRICK.BRICK2_NBR;
                    }
                    break;
                case 2:
                    switch (x)
                    {
                        case 0:
                            return (int)BRICK.BRICK3_BR;
                        case 1:
                            return (int)BRICK.BRICK3_GR;
                        case 2:
                            return (int)BRICK.BRICK3_NBR;
                    }
                    break;
            }

            return 0;
        }

        public override Bitmap GetBitmap()
        {
            if (!checkType(ID))
            {
                return null;
            }
            var cropRect = new Rectangle(0, 0, MainForm.FileSize * Width, MainForm.FileSize * Height);
            switch (ID)
            {
                case (int)BRICK.BRICK1_BR:
                    cropRect = new Rectangle(0, 0, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK1_GR:
                    cropRect = new Rectangle(MainForm.FileSize, 0, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK1_NBR:
                    cropRect = new Rectangle(MainForm.FileSize * 2, 0, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK2_BR:
                    cropRect = new Rectangle(0, MainForm.FileSize, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK2_GR:
                    cropRect = new Rectangle(MainForm.FileSize, MainForm.FileSize, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK2_NBR:
                    cropRect = new Rectangle(MainForm.FileSize * 2, MainForm.FileSize, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK3_BR:
                    cropRect = new Rectangle(0, MainForm.FileSize * 2, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK3_GR:
                    cropRect = new Rectangle(MainForm.FileSize, MainForm.FileSize * 2, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICK.BRICK3_NBR:
                    cropRect = new Rectangle(MainForm.FileSize * 2, MainForm.FileSize * 2, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
            }

            var src = Properties.Resources.Brick;
            var trg = new Bitmap(Width * MainForm.TileSize, Height * MainForm.TileSize);
            using (var g = Graphics.FromImage(trg))
            {
                g.DrawImage(src, new Rectangle(0, 0, trg.Width, trg.Height), cropRect, GraphicsUnit.Pixel);
            }
            return trg;
        }

    }
}
